home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 2
/
Gold Medal Software Volume 2 (Gold Medal) (1994).iso
/
windows
/
comms
/
recomm10.arj
/
RECOMNET.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-12
|
17KB
|
792 lines
#define MAIN
#define CFILES
#include "recomnet.h"
#include <stdlib.h>
#include <dos.h>
#define WM_NB_RESPONSE WM_USER +2000
#include <dblevel.h>
#define WM_DEBUG WM_USER + 101
BOOL IN_DEBUG = FALSE;
BOOL FAR __cdecl CL_DEBUG( UINT DebugNr, char huge *szFormat, ...)
{
static char szBuffer[200];
HWND hDebug;
LPSTR pArgs;
hDebug = FindWindow("HKR_DEBUG_WND", NULL);
if(hDebug == (HWND)NULL)
{
return FALSE;
}
if(IN_DEBUG == TRUE) return FALSE;
IN_DEBUG = TRUE;
pArgs = (LPSTR)&szFormat + sizeof(szFormat);
wvsprintf( szBuffer, szFormat, pArgs );
SendMessage(hDebug, WM_DEBUG, DebugNr, (LPARAM)(LPSTR)szBuffer);
IN_DEBUG = FALSE;
return TRUE;
}
typedef struct {
BYTE cLSN;
BYTE cNum;
char szName[17];
char szServer[80];
BYTE Adapter;
} DATA, FAR * LPDATA;
DATA clData;
int net_errno;
BOOL NET_INIT = FALSE;
#pragma optimize( "", off)
void getNetBiosName(LPSTR name)
{
register x, y;
extern void FAR PASCAL DOS3Call();
x = _FP_SEG(name);
__asm mov ds, x;
y = _FP_OFF(name);
__asm mov dx, y;
__asm mov ah, 0x5e ;
__asm mov al, 0x00 ;
DOS3Call();
}
#pragma optimize( "", on)
ERROR_MSG( LPSTR fnctxt)
{
char errtxt[200];
wsprintf(errtxt, "FEHLER: WΣhrend %s trat\n%s auf.", (LPSTR)fnctxt, (LPSTR)WNB_Error());
return (MessageBox(NULL, errtxt, APPTITLE,
MB_ABORTRETRYIGNORE|MB_ICONHAND));
}
void setReadyFlag()
{
FARPROC lpfn;
HINSTANCE hLib;
hLib = LoadLibrary("COMM");
if(hLib > HINSTANCE_ERROR)
{
lpfn = (FARPROC)GetProcAddress(hLib, "setRemoteRunning");
if(lpfn != (FARPROC)NULL)
{
(*lpfn)(event);
return;
}
}
MessageBox(NULL, "ACHTUNG: Beim benutzen von RemoteCommPort\n"
"trat folgender Fehler auf:\n"
"COMM.DRV not found", APPTITLE,
MB_OK|MB_ICONHAND);
}
exitDlg(HWND hDlg, HBRUSH hBrush, LPNCB brdNCB, UINT status)
{
KillTimer(hDlg, 2);
DeleteObject(hBrush);
if(brdNCB != NULL)
{
Cancel(clData.Adapter, brdNCB);
}
// CL_DEBUG(DB_ACTION, "CLIENT: HOSTDLG CLOSE");
EndDialog(hDlg, status);
return TRUE;
}
BOOL FAR PASCAL HostDlgProc(HWND hDlg, WORD message,
WORD wParam, LONG lParam)
{
static char hostaddr[80];
char tmp[80];
int rc;
static int timer;
DWORD x;
static HBRUSH hBrush;
static LPNCB fndNCB = NULL;
int lpTab[4] =
{ 20, 80, 110, 220
};
switch (message)
{
case WM_INITDIALOG:
clData.szServer[0] = '\0';
if(GetPrivateProfileString("HOST", "USE", "", hostaddr,
sizeof(hostaddr), "recomm.ini") > 0)
{
// CL_DEBUG(DB_ACTION, "CLIENT: FIX HOST %s", (LPSTR)hostaddr);
lstrcpy(clData.szServer, hostaddr);
EndDialog(hDlg, IDOK);
return TRUE;
}
hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
SendDlgItemMessage(hDlg, IDD_LISTE, LB_RESETCONTENT, 0, 0L);
SendDlgItemMessage (hDlg, IDD_LISTE, LB_SETTABSTOPS,
(WPARAM) 4,(LPARAM) (int huge *) lpTab);
timer = 10;
wsprintf(tmp, "%d", timer);
SetDlgItemText(hDlg, IDD_TIMER, tmp);
SetTimer(hDlg, 2, 2000, 0L);
rc = PostReceiveDatagram(clData.Adapter, clData.cNum,
(LPSTR)&fndSrv, sizeof(fndSrv), hDlg, WM_NB_RESPONSE, 1, &fndNCB);
SendBroadcast(WAIT_WND_MODAL, clData.Adapter, clData.cNum, (LPSTR)&fndSrv,
sizeof(fndSrv), hDlg);
return TRUE;
case WM_NB_RESPONSE:
lpNCB = (LPNCB) lParam;
if(lpNCB->cCmdCplt)
{
// CL_DEBUG(DB_ACTION, "FIND SERVER: NB_RESPONSE (0x%x)", lpNCB->cCmdCplt);
DestroyNCB(lpNCB);
return 0;
}
switch(lpNCB->cCommand & ~NO_WAIT)
{
case NETBIOS_RECEIVE_DATAGRAM:
lstrcpy(tmp, fndSrv.name);
if(fndSrv.stat == TRUE)
{
lstrcat(tmp, "\t belegt");
}
DestroyNCB(lpNCB);
rc = PostReceiveDatagram(clData.Adapter, clData.cNum,
(LPSTR)&fndSrv, sizeof(fndSrv), hDlg, WM_NB_RESPONSE, 1, &fndNCB);
SendDlgItemMessage(hDlg, IDD_LISTE, LB_ADDSTRING, 0, (LPARAM)(LPSTR)tmp);
timer = 10;
wsprintf(tmp, "%d", timer);
SetDlgItemText(hDlg, IDD_TIMER, tmp);
KillTimer(hDlg, 2);
SetTimer(hDlg, 2, 2000, 0L);
if(GetPrivateProfileString("HOST", "LAST", "", tmp,
sizeof(tmp), "recomm.ini") > 0)
{
x = SendDlgItemMessage(hDlg, IDD_LISTE, LB_SELECTSTRING, (WPARAM)-1,(LPARAM)(LPSTR)tmp);
SendDlgItemMessage(hDlg, IDD_LISTE, LB_SETCURSEL,(WPARAM)x, 0L);
}
return TRUE;
}
DestroyNCB(lpNCB);
return TRUE;
case WM_CTLCOLOR:
switch(HIWORD(lParam))
{
case CTLCOLOR_DLG:
case CTLCOLOR_STATIC:
SetBkColor((HDC)wParam, GetSysColor(COLOR_BTNFACE));
return(hBrush);
}
return 0L;
case WM_TIMER:
wsprintf(tmp, "%d", --timer);
SetDlgItemText(hDlg, IDD_TIMER, tmp);
if(timer == 0)
{
x = SendDlgItemMessage(hDlg, IDD_LISTE, LB_GETCURSEL, 0, (LPARAM)0L);
if(x == LB_ERR)
{
// CL_DEBUG(DB_ACTION, "CLIENT: TIMEOUT & NO HOST");
EndDialog(hDlg, IDCANCEL);
return TRUE;
}
SendDlgItemMessage(hDlg, IDD_LISTE, LB_GETTEXT, (WPARAM)x, (LPARAM)(LPSTR)hostaddr);
lstrcpy(clData.szServer, hostaddr);
// CL_DEBUG(DB_ACTION, "CLIENT: TIMEOUT HOST %s", (LPSTR)hostaddr);
exitDlg(hDlg, hBrush, fndNCB, IDOK);
return TRUE;
}
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
case IDD_LISTE:
if( (HIWORD(lParam) == LBN_DBLCLK)
|| (wParam == IDOK))
{
x = SendDlgItemMessage(hDlg, IDD_LISTE, LB_GETCURSEL, 0, (LPARAM)0L);
SendDlgItemMessage(hDlg, IDD_LISTE, LB_GETTEXT, (WPARAM)x, (LPARAM)(LPSTR)hostaddr);
WritePrivateProfileString("HOST", "LAST", hostaddr, "recomm.ini");
lstrcpy(clData.szServer, hostaddr);
// CL_DEBUG(DB_ACTION, "CLIENT: HOST %s", (LPSTR)hostaddr);
exitDlg(hDlg, hBrush, fndNCB, IDOK);
}
return TRUE;
case IDCANCEL:
exitDlg(hDlg, hBrush, fndNCB, IDCANCEL);
return TRUE;
}
break;
}
return FALSE;
}
handleMessage()
{
MSG msg;
while(PeekMessage(&msg, HwnD, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) return FALSE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TRUE;
}
conectToHost()
{
int rc;
CONECT_RETRY:
// CL_DEBUG(DB_ACTION, "CLIENT: WAIT CONNECT (%s)", (LPSTR)clData.szServer);
CONECTED = FALSE;
rc = Call(WAIT_APP_MODAL, clData.szName, clData.szServer,
clData.Adapter, 30, 30, HwnD, (LPBYTE)&clData.cLSN);
if(rc != 0)
{
net_errno = rc;
// CL_DEBUG(DB_ACTION, "CLIENT: CALL ERROR %s", (LPSTR)WNB_Error());
return FALSE;
}
// CL_DEBUG(DB_ACTION, "CLIENT: WAIT RESPONSE");
if((rc = readAnswer()) != 0)
{
// CL_DEBUG(DB_ACTION, "CLIENT: CONNECT DATA %s", (LPSTR)WNB_Error());
CONECTED = FALSE;
if(rc == IDRETRY) goto CONECT_RETRY;
return FALSE;
}
CONECTED = TRUE;
evtick = GetTickCount();
event = 0;
SetWindowText(HwnD, clData.szServer);
// CL_DEBUG(DB_ACTION, "CLIENT: CONECT OK");
return TRUE;
}
int FAR PASCAL initRecomNet()
{
HWND hwnd;
HMENU hmenu;
char tmp[30];
char * c1;
int rc;
hmenu = CreateMenu();
AppendMenu(hmenu, MF_STRING, IDABORT, "Disconect");
HwnD = hwnd = CreateWindow(szAppName, APPTITLE,
WS_OVERLAPPEDWINDOW, 0, 0, 30, 100, HWND_DESKTOP, hmenu, hLibrary, NULL);
// CL_DEBUG(DB_ACTION, "CLIENT HWND %d", hwnd);
ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
if(NET_INIT == FALSE)
{
NET_INIT = !NET_INIT;
clData.Adapter = 0;
getNetBiosName(tmp);
wsprintf(clData.szName, "RC_%12s", (LPSTR)tmp);
c1 = (clData.szName + sizeof(clData.szName) -1);
while(*c1 == ' ') *c1-- = '\0';
clData.szName[sizeof(clData.szName)] = '\0';
rc = AddName(WAIT_APP_MODAL, clData.szName, clData.Adapter, hwnd, &clData.cNum);
if(rc != 0)
{
DeleteName(WAIT_WND_MODAL, clData.szName, clData.Adapter, HwnD);
rc = AddName(WAIT_APP_MODAL, clData.szName, clData.Adapter, hwnd, &clData.cNum);
}
if(rc != 0)
{
net_errno = rc;
// CL_DEBUG(DB_ACTION, "CLIENT: CONNECT ERROR %s", (LPSTR)WNB_Error());
HwnD = NULL;
DestroyWindow(hwnd);
return FALSE;
}
}
return TRUE;
}
endConection()
{
// CL_DEBUG(DB_ACTION, "CLIENT: BYE BYE");
KillTimer(HwnD, 1);
CancelAllPending(HwnD);
HangUp(WAIT_APP_MODAL, clData.Adapter, clData.cLSN, HwnD);
SetWindowPos(HwnD, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
return TRUE;
}
int FAR PASCAL reCommCmd(struct recComData huge * lpComData)
{
int rc;
char far * c1;
// CL_DEBUG(DB_ACTION, "CLIENT: CMD %d=%s", lpComData->fnc, (LPSTR)cmd[lpComData->fnc]);
if(CONECTED == FALSE)
{
// CL_DEBUG(DB_ACTION, "CLIENT: NOT CONECTED");
CancelAllPending(HwnD);
HangUp(WAIT_APP_MODAL, clData.Adapter, clData.cLSN, HwnD);
if(IsWindow(HwnD) == 0)
{
if(initRecomNet() == FALSE)
{
MessageBox(NULL, "ACHTUNG: Es trat ein schwerwiegender\n"
"Netzwerkfehler auf. RemoteCommPort ist nicht m÷glich!\n"
, APPTITLE, MB_OK|MB_ICONHAND);
return FALSE;
}
}
if(lpComData->fnc == TRMCOM)
{
comData.rc = 0;
_fmemcpy(lpComData, &comData, sizeof(comData));
return TRUE;
}
if(lpComData->fnc == SETQUE)
{ // New Connect
if(SetTimer(HwnD, 1, 10, NULL) == NULL)
{
MessageBox(NULL, "ACHTUNG: Alle TimerResourcen sind derzeit belegt.\n"
"RemoteCommPort ist momentan nicht m÷glich!\n"
, APPTITLE, MB_OK|MB_ICONHAND);
endConection();
return FALSE;
}
tick = GetTickCount();
comData.fnc = CONNECT;
// CL_DEBUG(DB_ACTION, "CLIENT: START CONNECT FOR TASK %d", GetCurrentTask());
CONECTED = FALSE;
if(DialogBox(hLibrary, "HOSTS", NULL, HostDlgProc) == IDOK)
{
clData.szServer[17] = '\0';
c1 = _fstrchr(clData.szServer, '\t');
if(c1 != NULL) *c1 = '\0';
if(conectToHost() == TRUE)
{
CONECTED = TRUE;
}
}
if(CONECTED == FALSE)
{
MessageBox(NULL,
"Das Modem ist momentan belegt oder der Server lΣuft nicht.\n"
"Die Verbindung kann nicht aufgebaut werden.\n"
"Beheben Sie den Fehler und versuchen Sie es spΣter erneut"
, APPTITLE, MB_OK|MB_ICONHAND);
KillTimer(HwnD, 1);
endConection();
return FALSE;
}
}else
{
return FALSE;
}
// CL_DEBUG(DB_ACTION, "CLIENT: CONNECT DONE");
}
tick = GetTickCount();
_fmemcpy(&comData, lpComData, sizeof(comData));
rc = analizeMessage();
_fmemcpy(lpComData, &comData, sizeof(comData));
return (rc);
}
long FAR PASCAL ReCommWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_NB_RESPONSE:
lpNCB = (LPNCB) lParam;
if(lpNCB->cCmdCplt)
{
net_errno = lpNCB->cCmdCplt;
// CL_DEBUG(DB_ACTION, "CLIENT: RESPONSE ERROR %s", (LPSTR)WNB_Error());
CONECTED = FALSE;
endConection();
DestroyNCB(lpNCB);
return 0;
}
DestroyNCB(lpNCB);
return 0;
case WM_CREATE:
HwnD = hwnd;
memset(¬i, 0, sizeof(noti));
setReadyFlag();
// CL_DEBUG(DB_ACTION, "CLIENT: INIT DONE");
return TRUE;
case WM_QUERYENDSESSION:
return 1;
case WM_ENDSESSION:
exitDll();
return 1;
case WM_SYSCOMMAND:
if( (wParam == SC_MAXIMIZE)
|| (wParam == SC_RESTORE))
{
return 0;
}
break;
case WM_TIMER:
tack = GetTickCount();
if((tack - tick) > 120000)
{
if(CONECTED == TRUE)
{
tick = tack;
// CL_DEBUG(DB_ACTION, "CLIENT: RESET BY TIME OUT");
if(MessageBox(NULL, "Ein TimeOut ist aufgetreten\n"
"Soll ihre Verbindung abgebrochen werden ?",
APPTITLE, MB_RETRYCANCEL|MB_ICONHAND) == IDCANCEL)
{
CONECTED = FALSE;
CloseComm(REMOTE_COM);
endConection();
}
}
}
if( ( INTRANS == FALSE)
&& ((tack - evtick) > poll))
{
if(CONECTED == TRUE)
{
// CL_DEBUG(DB_ACTION, "CLIENT: poll event mask");
comData.fnc = GETMASK;
analizeMessage();
setReadyFlag(); // neue event mask an comm.drv
}
}
return TRUE;
case WM_COMMAND:
if(wParam == IDABORT)
{
endConection();
}
return TRUE;
case WM_CLOSE:
CONECTED = FALSE;
// CL_DEBUG(DB_ACTION, "CLIENT: DISCONECT");
endConection();
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
NET_INIT = FALSE;
CancelAllPending(hwnd);
DeleteName(WAIT_WND_MODAL, clData.szName, clData.Adapter, HwnD);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
analizeMessage()
{
int rc;
// PRE ACTION'S
// CL_DEBUG(DB_ACTION, "CLIENT: ANALIZE CMD %d=%s", comData.fnc, (LPSTR)cmd[comData.fnc]);
if(comData.fnc == TRMCOM)
{
poll = 5000;
sendCMD();
comData.rc = 0;
CONECTED = FALSE;
endConection();
return TRUE;
}
if(comData.fnc == CWSTRING)
{
cmdsize[comData.fnc] = SC + comData.len;
}
rc = sendDgram();
// POST ACTION'S ---------------------
if(event != comData.evmask)
{
if(noti.hwnd != NULL)
{
PostMessage(noti.hwnd, WM_COMMNOTIFY, REMOTE_COM,
MAKELONG(comData.evmask, 0));
// CL_DEBUG(DB_ACTION, "CLIENT: WM_COMMNOTIFY %d", comData.evmask);
}
}
event = comData.evmask;
if(comData.fnc == INICOM)
{
REMOTE_COM = comData.a.dcb.Id;
}
if(comData.fnc == CEVTGET)
{
event = comData.evmask;
}
if(comData.fnc == ENABLENOTI)
{
_fmemcpy(¬i, &comData.a.noti, sizeof(noti));
if(comData.a.noti.hwnd == NULL)
{
poll = 5000;
}else
{
poll = 100;
}
}
// CL_DEBUG(DB_ACTION, "CLIENT: rc=%d net_rc=%lu ev=%d", rc, comData.rc, event);
return rc;
}
sendDgram()
{
if(sendCMD() == 0)
{
while(readAnswer() == IDRETRY)
{
if(sendCMD() == FALSE)
{
return FALSE;
}
}
}else
{
return FALSE;
}
return TRUE;
}
readAnswer()
{
int rc;
int len;
evtick = tick = GetTickCount();
INTRANS = TRUE;
len = sizeof(comData);
// rc = ReceiveDatagram(WAIT_APP_MODAL, clData.Adapter, clData.cNum,
// (LPSTR)&comData, (LPWORD)&len, HwnD, clData.szServer);
rc = Receive(WAIT_APP_MODAL, clData.Adapter, clData.cLSN,
(LPSTR)&comData, (LPWORD)&len, HwnD);
if(rc != 0)
{
net_errno = rc;
// CL_DEBUG(DB_ACTION, "CLIENT(%s): READ %s", (LPSTR)cmd[comData.fnc], (LPSTR)WNB_Error());
rc = ERROR_MSG("READ DATA");
if(rc == IDABORT)
{
// CL_DEBUG(DB_ACTION, "CLIENT(%s): BREAKING", (LPSTR)cmd[comData.fnc]);
CONECTED = FALSE;
INTRANS = FALSE;
endConection();
return IDABORT;
}
if(rc == IDRETRY)
{ INTRANS = FALSE;
return(IDRETRY); // Retry
}
// Ignore
}
INTRANS = FALSE;
evtick = tick = GetTickCount();
return 0;
}
sendCMD()
{
int rc;
RETRY:
evtick = tick = GetTickCount();
INTRANS = TRUE;
// rc = SendDatagram(WAIT_APP_MODAL, clData.Adapter, clData.cNum, clData.szServer,
// (LPSTR)&comData, cmdsize[comData.fnc], HwnD);
rc = Send(WAIT_APP_MODAL, clData.Adapter, clData.cLSN,
(LPSTR)&comData, cmdsize[comData.fnc], HwnD);
if(rc != 0)
{
net_errno = rc;
// CL_DEBUG(DB_ACTION, "CLIENT(%s): SEND %s", (LPSTR)cmd[comData.fnc], (LPSTR)WNB_Error());
rc = ERROR_MSG("SEND DATA");
// Abort --------------------------
if(rc == IDABORT)
{
// CL_DEBUG(DB_ACTION, "CLIENT(%s): BREAKING", (LPSTR)cmd[comData.fnc]);
CONECTED = FALSE;
endConection();
INTRANS = FALSE;
return IDABORT;
}
// Retry --------------------------
if(rc == IDIGNORE) goto RETRY;
// Ignore --------------------------
}
evtick = tick = GetTickCount();
INTRANS = FALSE;
return 0;
}
initDll()
{
WNDCLASS wndclass;
lstrcpy(szAppName, APPNAME);
wndclass.style = CS_HREDRAW |CS_VREDRAW;
wndclass.lpfnWndProc = ReCommWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hLibrary;
wndclass.hIcon = LoadIcon(hLibrary, "recomm_ico");
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);
return TRUE;
}
void exitDll()
{
;
}